/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.dao.RoleDao;
import com.thinkgem.jeesite.modules.sys.entity.Role;
import com.thinkgem.jeesite.modules.sys.entity.SecurityLog;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SecurityLogService;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 授权日志Controller
 * @author fanqinglong
 * @version 2018-06-06
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/securityLog")
public class SecurityLogController extends BaseController {

	@Autowired
	private SecurityLogService securityLogService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private RoleDao roleDao;
	
	@ModelAttribute
	public SecurityLog get(@RequestParam(required=false) String id) {
		SecurityLog entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = securityLogService.get(id);
		}
		if (entity == null){
			entity = new SecurityLog();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:securityLog:view")
	@RequestMapping(value = {"list", ""})
	public String list(SecurityLog securityLog, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SecurityLog> page = securityLogService.findPage(new Page<SecurityLog>(request, response), securityLog); 
		model.addAttribute("page", page);
		return "modules/sys/securityLogList";
	}

	@RequiresPermissions("sys:securityLog:view")
	@RequestMapping(value = "form")
	public String form(SecurityLog securityLog, Model model) {
		model.addAttribute("securityLog", securityLog);
		//新增用户
		if ("1".equals(securityLog.getType())) {
			User user = (User) JsonMapper.fromJsonString(securityLog.getData(), User.class);
			user.setRoleList(roleDao.findList(new Role(user)));
			if (user.getCompany()==null || user.getCompany().getId()==null){
				user.setCompany(UserUtils.getUser().getCompany());
			}
			if (user.getOffice()==null || user.getOffice().getId()==null){
				user.setOffice(UserUtils.getUser().getOffice());
			}
			model.addAttribute("user", user);
			model.addAttribute("allRoles", systemService.findAllRole());
			return "modules/sys/userFormDisable";
		}else if("3".equals(securityLog.getType())){
			JsonMapper jsonMapper = JsonMapper.getInstance();
			Map<String, String> logDataMap = jsonMapper.fromJson(securityLog.getData(), jsonMapper.createCollectionType(Map.class, String.class,String.class));
			model.addAttribute("oldIds", logDataMap.get("old"));
			model.addAttribute("newIds", logDataMap.get("new"));
			model.addAttribute("menuList", systemService.findAllMenu());
			return "modules/sys/securityLogTree";
		}
		return "modules/sys/securityLogForm";
	}

	@RequiresPermissions("sys:securityLog:edit")
	@RequestMapping(value = "save")
	public String save(SecurityLog securityLog, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, securityLog)){
			return form(securityLog, model);
		}
		securityLogService.save(securityLog);
		addMessage(redirectAttributes, "保存日志成功");
		return "redirect:"+Global.getAdminPath()+"/sys/securityLog/?repage";
	}
	
	@RequiresPermissions("sys:securityLog:edit")
	@RequestMapping(value = "delete")
	public String delete(SecurityLog securityLog, RedirectAttributes redirectAttributes) {
		securityLogService.delete(securityLog);
		addMessage(redirectAttributes, "删除日志成功");
		return "redirect:"+Global.getAdminPath()+"/sys/securityLog/?repage";
	}

}